package com.serial.comm.modular.serialPort.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import com.serial.comm.core.constants.SystemConstants;
import com.serial.comm.core.reqres.response.ResponseData;
import com.serial.comm.modular.serialPort.enums.TelegramEnum;
import com.serial.comm.modular.serialPort.model.*;
import com.serial.comm.modular.serialPort.service.IOriginService;
import com.serial.comm.modular.serialPort.service.ITelegramIllegalService;
import com.serial.comm.modular.serialPort.service.ITelegramService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 串口读写报表 控制器
 */
@RestController
public class DataController {


    @Resource
    private ITelegramService telegramService;

    @Resource
    private ITelegramIllegalService telegramIllegalService;

    @Resource
    private IOriginService originService;

    public static Logger log = LoggerFactory.getLogger(DataController.class);

    @RequestMapping("/read/list")
    public ResponseData getSerialReadTable(TelegramVo telegramVo) {
        log.info(telegramVo.toString());
        Page<Telegram> page = new Page<>();
        page.setSize(telegramVo.getPageSize());
        page.setCurrent(telegramVo.getCurrent());
        List<Telegram> telegrams = telegramService.getTelegramList(page, telegramVo);
        page.setRecords(telegrams);
        return ResponseData.success(200, SystemConstants.Message.OPERATE_SUCCESS, page);
    }

    @RequestMapping("/read/errorList")
    public ResponseData getSerialErrorTable(@RequestParam("current") Integer current, @RequestParam("pageSize") Integer pageSize) {
        Page<TelegramIllegal> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(current);
        Page<TelegramIllegal> illegalTelegramList = telegramIllegalService.getIllegalTelegramList(page);
        return ResponseData.success(200, SystemConstants.Message.OPERATE_SUCCESS, illegalTelegramList);
    }

    @RequestMapping("/read/getOriginBySId")
    public ResponseData getOriginDataBySourceId(@RequestParam("sourceId") Integer sourceId) {
        Origin origin = originService.getOriginBySourceId(sourceId);
        String content = origin.getContent();
        TelegramMQ telegramMQ = JSON.parseObject(content, TelegramMQ.class);
        return ResponseData.success(200, SystemConstants.Message.OPERATE_SUCCESS, telegramMQ.getOriginalText());
    }


    @RequestMapping("/write/list")
    public ResponseData getSerialWriteTable(@RequestParam("current") Integer current, @RequestParam("pageSize") Integer pageSize) {
        Page<Origin> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(current);
        Page<Origin> writeList = originService.getWriteList(page, TelegramEnum.WRITE.getCode(), TelegramEnum.SUCCESS.getCode());
        return ResponseData.success(200, SystemConstants.Message.OPERATE_SUCCESS, writeList);
    }

    @RequestMapping("/write/errorList")
    public ResponseData getSerialWriteErrorTable(@RequestParam("current") Integer current, @RequestParam("pageSize") Integer pageSize) {
        Page<Origin> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(current);
        Page<Origin> writeList = originService.getWriteList(page, TelegramEnum.WRITE.getCode(), TelegramEnum.FAILED.getCode());
        return ResponseData.success(200, SystemConstants.Message.OPERATE_SUCCESS, writeList);
    }
}
